package sg.tua.game;

import java.util.TreeMap;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

import sg.tua.hud.HudAnimation;
import sg.tua.hud.HudAnimationFactory;
import sg.tua.hud.MoveableHudAnimation;

public class Kitchen {
	
	private Vector2f GridSlot1;
	private Vector2f GridDelta;
	private HudAnimation backGround;
	private TreeMap<String,MoveableHudAnimation> mealList;
	//private TreeMap<String,IngredientObject> ingredientList;

	public Kitchen(){
		GridSlot1 = new Vector2f(524,445);
		GridDelta = new Vector2f(127,115);
		mealList = new TreeMap<String,MoveableHudAnimation>();
		//ingredientList = new TreeMap<String,IngredientObject>();
	}
	public void setBackGround(String image) throws SlickException {
		this.backGround = HudAnimationFactory.create("Background", image, new Vector2f(0,0));
	}

	public HudAnimation getBackGround() {
		return backGround;
	}
	public void renderIngredients(Graphics g){
		
	}
	public void renderMeals(Graphics g){
		for (String i : mealList.keySet()){
			mealList.get(i).render(g);
		}
	}
	public void addIngredient(String name, String image){
		
	}
	public void addMeal(String name, String img) throws SlickException{
		 Image newImg = new Image(img);
		 Image[] image = {newImg.getSubImage(3, 5,  105, 90),newImg.getSubImage(112, 5, 105, 90)};
		 float gridX = mealList.size()%4;
	     float gridY = (mealList.size()-mealList.size()%4)/4;
	     float x = GridSlot1.x+gridX*GridDelta.x;
	     float y = GridSlot1.y+gridY*GridDelta.y;
	     Animation anim = new Animation(image,1);
	     anim.setAutoUpdate(false);
		 Vector2f position = new Vector2f(x, y);
		 Rectangle imageShape = new Rectangle(x, y,(float)image[0].getWidth(),(float)image[0].getHeight());
		 mealList.put(name, new MoveableHudAnimation(name,position,anim,imageShape,0,false));
	}
	public void updateMeals(GameContainer gc, StateBasedGame sb, int delta){
		for (String i : mealList.keySet()){
			mealList.get(i).update(gc, sb, delta);
		}		
	}
	
	public void RegisterMousListeners(GameContainer gc){
		for (String i : mealList.keySet()){
			gc.getInput().addMouseListener(mealList.get(i));
		}	
	}
	public MoveableHudAnimation getClicked(){
		for (String i : mealList.keySet()){
			if (mealList.get(i).isClicked()) return mealList.get(i);
		}
		return null;
	}
}
